package com.example.myapplication.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.myapplication.Activity.F1;
import com.example.myapplication.R;


public class fragment2 extends Fragment {

    private EditText editHeight;
    private EditText editWeight;
    private RadioButton rbBoy;
    private RadioButton rbGirl;
    private Button buttonStart;
    private Button buttonReset;

    public fragment2() {
        // Required empty public constructor
    }

    View view;
    Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_fragment2, container, false);
        context = getContext();
        initView();
        resetData();
        caclute();
        return view;
    }

    /*
        计算BMI值
         */
    public void caclute() {
        buttonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Double bm = bmi();
                if (bm == 0.0) {
                } else {
                    startActivity(new Intent(context, F1.class).putExtra("bmi", bm));

                }
            }
        });
    }

    private void initView() {
        editHeight = (EditText) view.findViewById(R.id.edit_height);
        editWeight = (EditText) view.findViewById(R.id.edit_weight);
        rbBoy = (RadioButton) view.findViewById(R.id.rb_boy);
        rbGirl = (RadioButton) view.findViewById(R.id.rb_girl);
        buttonStart = (Button) view.findViewById(R.id.button_start);
        buttonReset = (Button) view.findViewById(R.id.button_reset);
    }

    /*
进行bmi逻辑运算
 */
    public Double bmi() {
        Double weight;
        Double height;
        if (editWeight.getText().toString().equals("") || editHeight.getText().toString().equals("")) {
            Toast.makeText(context, "身高体重不能为空", Toast.LENGTH_SHORT).show();
            return 0.0;
        } else {
            weight = Double.valueOf(editWeight.getText().toString());
            height = Double.valueOf(editHeight.getText().toString());
            Double b = weight / height / height;
            return b;
        }
    }

    /*
    重置所有数据
     */
    public void resetData() {
        buttonReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editHeight.setText("");
                editWeight.setText("");
                ;
                rbBoy.setChecked(true);
            }
        });
    }
}
